﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace RestServices
{
    public class ServiceCall
    {
        public static T MakeRequestPost<T>(string requestUrl)
        {
            try
            {
                string postContent = requestUrl.Substring(requestUrl.IndexOf('?') + 1);
                string url = requestUrl.Substring(0, requestUrl.IndexOf('?'));

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.ContentLength = postContent.Length;
                request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(postContent, 0, postContent.Length);
                }

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));

                    DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
                    object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                    T jsonResponse = (T)objResponse;
                    return jsonResponse;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return default(T);
            }
        }

        public static T MakeRequestGet<T>(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));

                    //Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    //StreamReader loResponseStream = new
                    //  StreamReader(response.GetResponseStream(), enc);

                    //string Response = loResponseStream.ReadToEnd();

                    //loResponseStream.Close();

                    DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
                    object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                    T jsonResponse = (T)objResponse;
                    return jsonResponse;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return default(T);
            }
        }

        public static string MakeRequestDelete(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                request.Method = "DELETE";
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));

                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader loResponseStream = new
                      StreamReader(response.GetResponseStream(), enc);

                    string Response = loResponseStream.ReadToEnd();

                    loResponseStream.Close();

                    return Response;
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public static string MakeRequestGet(string requestUrl)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                        throw new Exception(String.Format(
                        "Server error (HTTP {0}: {1}).",
                        response.StatusCode,
                        response.StatusDescription));

                    Encoding enc = System.Text.Encoding.GetEncoding(1252);
                    StreamReader loResponseStream = new
                      StreamReader(response.GetResponseStream(), enc);

                    string Response = loResponseStream.ReadToEnd();

                    loResponseStream.Close();

                    return Response;
                }
            }
            catch (Exception e)
            {
                throw new Exception("MakeRequestGet:" + e.Message, e);
            }
        }
    }
}
